package org.akomantoso.core.data;

import java.io.IOException;

import javax.xml.datatype.Duration;

import org.akomantoso.core.data.attribute.RefersReq;
import org.akomantoso.core.io.Input;
import org.akomantoso.core.io.Output;
import org.akomantoso.core.io.OutputUtil;

/**
 * <pre>
 * 	<xsd:element name="timeInterval">
 * 		<xsd:complexType>
 * 			<xsd:complexContent>
 * 				<xsd:extension base="metaopt">
 * 					<xsd:attributeGroup ref="refersreq"/>
 * 					<xsd:attribute name="start" type="xsd:anyURI"/>
 * 					<xsd:attribute name="end" type="xsd:anyURI"/>
 * 					<xsd:attribute name="duration" type="xsd:duration"/>
 *				</xsd:extension>
 *			</xsd:complexContent>
 * 		</xsd:complexType>
 * 	</xsd:element>
 * </pre>
 * 
 * @author <a href="mailto:jacques.militello@gmail.com">Jacques Militello</a>
 * 
 * @since http://www.akomantoso.org/1.0
 * 
 */
public final class TimeInterval extends MetaOpt implements RefersReq {
	
	public static final String ELEMENT = "timeInterval";
	
	public static final String ATTRIBUE_START = "start";
	public static final String ATTRIBUE_END = "end";
	
	private String _start;
	private String _end;
	private Duration _duration;
	private String _refersTo;

	public String getStart() {
		return _start;
	}

	public void setStart(String start) {
		_start = start;
	}

	public String getEnd() {
		return _end;
	}

	public void setEnd(String end) {
		_end = end;
	}

	public Duration getDuration() {
		return _duration;
	}

	public void setDuration(Duration duration) {
		_duration = duration;
	}

	public String getRefersTo() {
		return _refersTo;
	}

	public void setRefersTo(String refersTo) {
		_refersTo = refersTo;
	}
	
	/**
	 * @see org.akomantoso.core.data.MetaOpt#writeExternal(org.akomantoso.core.io.Output)
	 */
	@Override
	public <T extends Output> void writeExternal(T out) throws IOException {
		out.writeStart(ELEMENT);
		OutputUtil.writeRefersReq(out, this);
		OutputUtil.writeAttribute(out, "start", _start);
		OutputUtil.writeAttribute(out, "end", _end);
		if (_duration != null) {
			OutputUtil.writeAttribute(out, "duration", _duration.toString());
		}
		super.writeExternal(out);
		out.writeEnd();
	}

	/**
	 * @see org.akomantoso.core.data.MetaOpt#readExternal(org.akomantoso.core.io.Input)
	 */
	@Override
	public <T extends Input> void readExternal(T in) throws IOException {
		_refersTo = in.readAttribute(RefersReq.ATTRIBUTE);
		_start = in.readAttribute(ATTRIBUE_START);
		_end = in.readAttribute(ATTRIBUE_END);
		super.readExternal(in);
	}
	
}